package com.ocom.xfrecode.controller;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ocom.common.constant.RecodeUrl;
import com.ocom.common.entity.payconfig.EntityMqttOperation;
import com.ocom.common.request.emqx.TimerMqttOrderRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.xfrecode.constant.ShardingConstant;
import com.ocom.xfrecode.service.XfMqttOperationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
public class MqttOperationController {
    @Autowired
    private XfMqttOperationService xfMqttOperationService;

    @PostMapping(RecodeUrl.URL_saveOrUpdateMqttOperation)
    public Result addMqttOperation(@RequestBody EntityMqttOperation entityMqttOperation){
        if(ObjectUtil.isEmpty(entityMqttOperation.getMonthValue())){
            Date date = ObjectUtil.isEmpty(entityMqttOperation.getCreateTime())?new Date():entityMqttOperation.getCreateTime();
            entityMqttOperation.setMonthValue(DateUtil.format(date, ShardingConstant.DATE_SHARDING_TABLE_FORMAT));
        }
        this.xfMqttOperationService.saveOrUpdate(entityMqttOperation);
        return ResultUtil.success();
    }

    @PostMapping(RecodeUrl.URL_updateMqttOperation)
    public Result updateMqttOperation(@RequestBody EntityMqttOperation entityMqttOperation){
        if(ObjectUtil.isEmpty(entityMqttOperation.getMonthValue())){
            entityMqttOperation.setMonthValue(DateUtil.format(new Date(), ShardingConstant.DATE_SHARDING_TABLE_FORMAT));
        }
        this.xfMqttOperationService.update(entityMqttOperation,Wrappers.lambdaQuery(EntityMqttOperation.class)
                .eq(EntityMqttOperation::getId,entityMqttOperation.getId())
                .eq(EntityMqttOperation::getMonthValue,entityMqttOperation.getMonthValue())
        );
        return ResultUtil.success();
    }

    @PostMapping(RecodeUrl.URL_detailMqttOperation)
    public Result detailMqttOption(@RequestBody EntityMqttOperation entityMqttOperation){
        if(ObjectUtil.isEmpty(entityMqttOperation.getMonthValue())){
            entityMqttOperation.setMonthValue(DateUtil.format(new Date(), ShardingConstant.DATE_SHARDING_TABLE_FORMAT));
        }
        entityMqttOperation = this.xfMqttOperationService.getOne(Wrappers.lambdaQuery(EntityMqttOperation.class)
                .eq(EntityMqttOperation::getComId,entityMqttOperation.getComId())
                .eq(EntityMqttOperation::getMonthValue,entityMqttOperation.getMonthValue())
                .eq(ObjectUtil.isNotEmpty(entityMqttOperation.getMqttDeviceId()),EntityMqttOperation::getMqttDeviceId,entityMqttOperation.getMqttDeviceId())
                .eq(ObjectUtil.isNotEmpty(entityMqttOperation.getMqttDeviceSub()),EntityMqttOperation::getMqttDeviceSub,entityMqttOperation.getMqttDeviceSub())
                .isNull(ObjectUtil.isEmpty(entityMqttOperation.getEndTime()),EntityMqttOperation::getEndTime)
        );
        return ResultUtil.success(entityMqttOperation);
    }


    @PostMapping(RecodeUrl.URL_timerMqttOrder)
    public Result timerMqttOrder(@RequestBody TimerMqttOrderRequest request){
        if(ObjectUtil.isEmpty(request.getMonthValue())){
            request.setMonthValue(DateUtil.format(new Date(), ShardingConstant.DATE_SHARDING_TABLE_FORMAT));
        }
        int sec = -7200;
        List<EntityMqttOperation> entityMqttOperations = this.xfMqttOperationService.list(
                Wrappers.lambdaQuery(EntityMqttOperation.class)
                        .eq(EntityMqttOperation::getMonthValue,request.getMonthValue())
                        .eq(EntityMqttOperation::getState,request.getOrderState())
                        .le(EntityMqttOperation::getUpdateTime, DateUtil.offset(new Date(), DateField.SECOND, -sec))
        );
        //修改状态
        for (EntityMqttOperation entityMqttOperation:entityMqttOperations){
            entityMqttOperation.setEndTime(new Date());
            this.xfMqttOperationService.saveOrUpdate(entityMqttOperation);
        }
        return ResultUtil.success(entityMqttOperations);
    }
}
